'use strict';

/**
 * @name 修改状态操作
 * @description params.data.state为现在状态, params.data.module为需求还是技术
 * @author ZYF
 */

angular.module('metalabOperationApp').controller('StateModelCtrl', ['$rootScope', '$stateParams', '$state', 'params', '$scope', '$uibModalInstance', 'MetaResources', 'MetaRemind', 'MetaConstants', function($rootScope, $stateParams, $state, params, $scope, $uibModalInstance, MetaResources, MetaRemind, MetaConstants) {
    $scope.stateCtrl = {
      states: [],
      choice: ''
    };
    $scope.sure = function() {
      var verified = null;
      if ($scope.stateCtrl.choice==='征集中' || $scope.stateCtrl.choice==='推广中') {
        verified = true;
      } else if ($scope.stateCtrl.choice==='不通过'){
        verified = false;
      }
      MetaResources[params.data.module].save({
        'id': $stateParams.id
      }, {
        'state': $scope.stateCtrl.choice,
        'verified': verified,
      }, function() {
          MetaRemind.success("恭喜", "修改成功");
          $uibModalInstance.dismiss('cancel');
          $state.reload();
      },
      function(error) {
          MetaRemind.serverError(error);
          $uibModalInstance.dismiss('cancel');
      });
    };
    $scope.cancel = function() {
        $uibModalInstance.dismiss('cancel');
    };
    $scope.init = function() {
      if (params.data.module==='demand') {
        $scope.stateCtrl.states = MetaConstants.demandState;
        $scope.title = "修改需求状态";
      } else {
        $scope.stateCtrl.states = MetaConstants.techState;
        $scope.title = "修改技术状态";
      }
      $scope.stateCtrl.choice = params.data.state;
    };
    $scope.init();
}]);
